package cc.cc4414.cc02.core.util;

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * json工具
 * 
 * @author cc 2018年5月5日
 */
public class JsonUtils {
	private static final ObjectMapper mapper = new ObjectMapper();

	/**
	 * 将json字符串解析为对象
	 */
	public static <T> T parse(String json, Class<T> clazz) {
		try {
			return mapper.readValue(json, clazz);
		} catch (IOException e) {
			throw new IllegalArgumentException("解析异常", e);
		}
	}

	/**
	 * 将对象格式化为json字符串
	 */
	public static String format(Object obj) {
		try {
			return mapper.writeValueAsString(obj);
		} catch (IOException e) {
			throw new IllegalArgumentException("格式化异常", e);
		}
	}
}
